;(function() {

  new Vue({
    el: '#expIndex',
    data: {
      data: [],
      billPaymentStateData: [{name: '已收',code: 'APE'},{name: '未收',code: 'WP'},{name: '部分',code: 'PPE'}],
      billOverTypeData: [{name: '即将到期',code: 'DU'},{name: '已逾期',code: 'OD'}],
      selectedPaymentStateCode: [],
      selectedOverStateCode: [],
      khname: '',
      expenseName: [],
      fkbegintime:null,
      fkendtime:null,
      szbegintime:null,
      szendtime:null,
      currentPage: 1,
      totalCount: 0,
      totalPage: 0,
      pageCount: ['10'],
      pages: [{name: '10', code: '10'}, {name: '15', code: '15'}, {name: '20', code: '20'}],
      cTdata: [],
      isExtenseFlag:false,
			bill: {},
			projectList: [],
			buildingList: [],
			floorList: [],
			unitList: [],
			projectIDs: [],
			projectId: [],
			buildingId: [],
			floorId: [],
			unitId: [],
			showView: false,
      groupId:null,
      groupOneData:[],
      groupTwoData:[],
      groupThreeData:[],
      selectedGroupData:[],

      breaksData:[
        {name:'减免本金',code:'A'},
        {name:'减免滞纳金',code:'L'},
      ],
      selectedBreaksCode:[],
    },
    filters: {
      overDay: function(value) {
        if (value < 0) {
          return days = '逾期' + Math.abs(value) + '天'
        } else {
          return days = parseFloat(value)
        }
      },
      billPaymentState: function(str) {
        return hlCodeConvert.billPaymentState(str)
      },
      formatNum:function(item){
        return hlNumberUtil.formatNum(item)
      }
    },
    methods: {
      goToCurSsMx() {
        console.log('222');
        hlUrlUtil.goTo('ssDetailsReport', {})
      },
      selectBillMenu(){
        this.currentPage = 1
        this.getData()
      },
      resetTabbar(){
        this.selectedPaymentStateCode = []
        this.selectedOverStateCode = []
        this.selectedBreaksCode = []
        this.currentPage = 1

        this.getData()
      },
      handleSelectedExpense (arr){
        this.expenseName = [arr[0].name]
        this.currentPage = 1
        this.getData()
      },
      openSearch(){
        if(this.isExtenseFlag == true){
          this.isExtenseFlag = false;
					this.projectId = []
					this.buildingId = []
					this.floorId = []
					this.unitId = []
					this.buildingList = []
					this.floorList = []
					this.unitList = []
          return false;
        }

        if(this.isExtenseFlag == false){
          this.isExtenseFlag = true;
          return false;
        }
      },
      search(){
        this.currentPage = 1
        this.getData()
      },
      //获取列表 默认nums（当前页）: 1, cost（当前费项）: '#'
      getData() {
        var self = this,projectIDsIn = [],projectIDs = [];

        this.getCTData(this.projectIDs.toString())
				this.expenseTotal()

        var data = {
          "keyword": self.khname.trim() || '#', //客户名，模糊匹配
					"project_ids": self.projectIDs.toString() || '#',
          'project_id': self.projectId.length ? self.projectId[0] : '#',
          'group_id':self.groupId || '#',
          'building_id': self.buildingId.length ? self.buildingId[0] : '#',
          'floor_id': self.floorId.length ? self.floorId[0] : '#',
          'unit_id': self.unitId.length ? self.unitId[0] : '#',
          "deadline_begin_date": self.fkbegintime || '#', //付款截止开始日期
          "deadline_end_date": self.fkendtime || '#', //付款截止结束日期
          "payment_begin_date": self.szbegintime || '#', //收款开始日期
          "payment_end_date": self.szendtime || '#', //收款结束日期
          "payment_state": self.selectedPaymentStateCode.length ? self.selectedPaymentStateCode.join() : '#',
          "overdue_state": self.selectedOverStateCode.length ? self.selectedOverStateCode.join() : '#',
          "derate_type": self.selectedBreaksCode.length ? self.selectedBreaksCode.join() : '#',
          "expenditure": self.expenseName.length ? self.expenseName[0] : '#', //费项
          "page": self.currentPage, //请求页
          "pageCount": self.pageCount[0] //每页数量
        }

        hlhttp.get({
          url: 'bills/expense/_search',
          data: data,
          success: function(res) {
            self.totalPage = res.totalPage
            self.totalCount = res.totalCount
            self.data = res.expenses;
          }
        })
      },
			//收支管理统计
			expenseTotal () {
				if (this.expenseName[0] == '全部') {this.expenseName = ['#']}

				var data = {
				  "keyword": this.khname.trim() || '#', //客户名，模糊匹配
					"project_ids": this.projectIDs.toString(),
				  "project_id": this.projectId.length ? this.projectId[0] : '#',
          "group_id":this.groupId || '#',
				  "building_id": this.buildingId.length ? this.buildingId[0] : '#',
				  "floor_id": this.floorId.length ? this.floorId[0] : '#',
				  "unit_id": this.unitId.length ? this.unitId[0] : '#',
				  "deadline_begin_date": this.fkbegintime || '#', //付款截止开始日期
				  "deadline_end_date": this.fkendtime || '#', //付款截止结束日期
				  "payment_begin_date": this.szbegintime || '#', //收款开始日期
				  "payment_end_date": this.szendtime || '#', //收款结束日期
				  "payment_state": this.selectedPaymentStateCode.toString() || '#', //支付状态
          "overdue_state": this.selectedOverStateCode.toString() || '#', //逾期状态，DU即将到期，OD已逾期
          "derate_type": this.selectedBreaksCode.length ? this.selectedBreaksCode.join() : '#',
				  "expenditure": this.expenseName.length ? this.expenseName[0] : '#', //费项
				  "page": this.currentPage, //请求页
				  "pageCount": this.pageCount[0] //每页数量
				}

				hlhttp.get({
				  url: 'bills/expense/_statistics',
				  data: data,
				  success: (res) => {
						this.bill = res;
				  }
				})
			},
      //重置
      resetBtn: function() {
        this.khname = '';
        this.fkbegintime = null
        this.fkendtime = null
        this.szbegintime = null
        this.szendtime = null
        this.navoneS = '#'
        this.navtwoS = '#'
        this.expenseName = []
				this.projectId = []
        this.groupId = null
				this.buildingId = []
				this.floorId = []
				this.unitId = []
				this.buildingList = []
				this.floorList = []
				this.unitList = []
        this.selectedGroupData = []
        this.currentPage = 1

        this.getData();
      },
      //获取费项
      getCTData: function(item) {
        var _this = this;
        hlhttp.get({
          url: 'project/expenditures',
          data: { project_id: item || '#' },
          success: function(res) {
						res.unshift({name: '全部', id: '#'})
            _this.cTdata = hlArrayUtil.replaceData(res, [{key: 'id', rKey: 'code'}]);
            console.log(_this.cTdata)
          }
        })
      },
      //控制显示数量
      pagesFY: function(arr) {
        const self = this;
				self.pageCount = [arr[0].code]
        self.currentPage = 1

        setTimeout(function() {
          self.getData()
        }, 200);
      },

			//获取项目
			getprojectData () {
        const self = this

			  hlhttp.get({
			    url: 'user/projects',
			    success: (res) => {
						res = hlArrayUtil.removeRepeat(res, 'projectId')
						self.projectList = hlArrayUtil.replaceData(res, [{key: 'projectName', rKey: 'name'}, {key: 'projectId', rKey: 'code'}])
						self.projectList.map( item => self.projectIDs.push(item.projectId) )
						self.getData()
			    }
			  })
			},
      // 获取一级组合数据
      getGroupPrimaryData(data){
        const self = this
        self.project_id = [data[0].code]
        self.groupId = null
        self.buildingId = []
        self.floorId = []
        self.unitId = []
        self.buildingList = []
        self.floorList = []
        self.unitList = []
        self.selectedGroupData = []
        self.getGroupData().then(data => {
          self.groupOneData = hlArrayUtil.replaceData( data, [{key: 'id', rKey: 'code'}] );
        })
        self.getBuildingData()
      },
      // 选取一级组合数据
      selectOneGroup(res){
        const self = this
        this.groupId = res['cur']['code']
        this.getGroupData().then(data => {
          self.groupTwoData = hlArrayUtil.replaceData( data, [{key: 'id', rKey: 'code'}] )
          self.getBuildingData()
        })
      },
      // 选取二级组合数据
      selectTwoGroup(res){
        const self = this
        this.groupId = res['cur']['code']
        this.getGroupData().then(data => {
          self.groupThreeData = hlArrayUtil.replaceData( data, [{key: 'id', rKey: 'code'}] )
          self.getBuildingData()
        })
      },
      // 选取三级组合数据
      selectThreeGroup(res){
        this.groupId = res['cur']['code']
      },
      getGroupData(){
        const self = this

        return new Promise(resolve => {
          hlhttp.get({
            url:'project/grouping_info/_subclass',
            data:{'project_id':self.project_id[0] , 'grouping_id':self.groupId || '#' },
            success:function(data){
              resolve(data)
            }
          })
        })
      },
			//获取楼宇
			getBuildingData () {
        const self = this
			  hlhttp.get({
			    url: 'project/grouping/_building',
			    data: { project_id: self.projectId.length ? self.projectId[0] : '#'  , grouping_id:self.groupId || '#'},
			    success: (data) => {
            data = data.filter(el => el.configState == 'Y')
			      this.buildingList = hlArrayUtil.replaceData(data, [{key: 'buildingId', rKey: 'code'}]);
			    }
			  })
			},
			//获取楼层
			getfloorData () {
			  hlhttp.get({
			    url: 'building/floors',
			    data: {project_id: this.projectId[0], building_id: this.buildingId[0]},
			    success: (res) => {
			      this.floorList = hlArrayUtil.replaceData(res, [{key: 'floorName', rKey: 'name'}, {key: 'floorId', rKey: 'code'}]);
			    }
			  })
			},
			//获取单元
			getunitData () {
			  hlhttp.get({
			    url: 'floor/units',
			    data: {project_id: this.projectId[0], building_id: this.buildingId[0], floor_id: this.floorId[0]},
			    success: (res) => {
			      this.unitList = hlArrayUtil.replaceData(res, [{key: 'unitName', rKey: 'name'}, {key: 'unitId', rKey: 'code'}]);
			    }
			  })
			},
			//点击项目获得  id
			getprojectId (arr) {
			  this.projectId = [arr[0].code];
			  this.buildingId = [];
			  this.floorId = [];
			  this.unitId = [];
        this.getBuildingData()
			},
			//点击楼宇获得 id
			getbuildingId (arr) {
			  this.buildingId = [arr[0].code];
			  this.floorId = [];
			  this.unitId = [];
        this.getfloorData()
			},
			//点击楼层获得 id
			getfloorId (arr) {
			  this.floorId = [arr[0].code];
			  this.getunitData()
			  this.unitId = [];
			},
			// 点击单元获得 id
			getunitId (arr) {
			  this.unitId = [arr[0].code];
			},
    },
    mounted: function() {
      if (window.localStorage.getItem('clickEQ')) {
        this.navtwoS = window.localStorage.getItem('clickEQ')
      }
      hlSelectUi(true)
			this.getprojectData()

    }
  })
}())
